package com.mms.action;

import java.io.IOException;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONArray;

import org.apache.struts2.ServletActionContext;

import com.mms.dao.InvitationDao;
import com.mms.dao.MyMeetingDao;
import com.mms.dao.PersonManagementDao;
import com.mms.entity.Invitation;
import com.mms.entity.InvitationId;
import com.mms.entity.Meeting;
import com.mms.entity.Staff;
import com.mms.util.MyMeetingInfo;
import com.opensymphony.xwork2.Action;

public class MyMeetingAction implements Action {

    private Integer[] SId;
    private String topic;
    private String reason;
    public String getReason() {
        return reason;
    }
    public void setReason(String reason) {
        this.reason = reason;
    }
    public String getTopic() {
        return topic;
    }
    public void setTopic(String topic) {
        this.topic = topic;
    }
    public Integer[] getSId() {
        return SId;
    }
    public void setSId(Integer[] sId) {
        SId = sId;
    }
    private String startTime;
    public String getStartTime() {
        return startTime;
    }
    public void setStartTime(String startTime) {
        this.startTime = startTime;
    }
    public String getEndTime() {
        return endTime;
    }
    public void setEndTime(String endTime) {
        this.endTime = endTime;
    }
    private String endTime;
   
    public String checkStaffFree() throws IOException {
        PersonManagementDao pmd=new PersonManagementDao();
        System.out.println(SId);
        System.out.println(startTime);
        List<String> strings=new ArrayList<String>();
        String result = "true";
        for (int s : SId) {
            if (pmd.isStaffFree(s, startTime, endTime)) {
                result="true";
            }
            else {result="false";break;}
        }
        strings.add(result);
        JSONArray jsonArray = JSONArray.fromObject(strings); 
        HttpServletResponse response=ServletActionContext.getResponse();
        response.setContentType("text/html;charset=utf-8");
        PrintWriter out = response.getWriter();
        System.out.println("result="+jsonArray.toString());
        out.print(jsonArray.toString());
        out.flush();
        out.close();
        return SUCCESS;
    }
    
    public String addInvitation() throws UnsupportedEncodingException {
        HttpServletRequest request=ServletActionContext.getRequest();
        request.setCharacterEncoding("UTF-8");
        InvitationDao invitationDao=new InvitationDao();
        Invitation invitation=new Invitation();
        invitation.setTopic(topic);
        invitation.setCheckFlag(0);
        for(int i=0;i<SId.length;i++){
            invitation.setSid(SId[i]);
            if (!invitationDao.isInvitationExist(invitation)) {
                InvitationId invitationId=new InvitationId();
                Staff staff=new Staff(SId[i]);
                Meeting meeting=new Meeting();
                meeting.setTopic(topic);
                invitationId.setStaff(staff);
                invitationId.setMeeting(meeting);
                invitation.setId(invitationId);
                if (invitationDao.addInvitation(invitation)) {
                    request.setAttribute("isInviteSuccessful", true);
                }
                else {
                    request.setAttribute("isInviteSuccessful", false);
                }
            }
            
        }
        
        request=MyMeetingInfo.getMyMeetingInfo(request);
        return SUCCESS;
    }
    
    public String cancelMeeting() throws UnsupportedEncodingException {
        HttpServletRequest request=ServletActionContext.getRequest();
        request.setCharacterEncoding("UTF-8");
        request=MyMeetingInfo.getMyMeetingInfo(request);
        MyMeetingDao myMeetingDao =new MyMeetingDao();
        myMeetingDao.cancelMeeting(topic, reason);
        return SUCCESS;
    }
    
    
    
    
    @Override
    public String execute() throws Exception {
        HttpServletRequest request=ServletActionContext.getRequest();
        request.setCharacterEncoding("UTF-8");
        request=MyMeetingInfo.getMyMeetingInfo(request);
        return SUCCESS;
    }

}
